import { Reducer, Effect } from 'umi';
import * as services from '../services/';
import { beJson } from './test';

// 登录返回值校验
export interface tableType {
  pageSize?: number;
  pageIndex?: number;
  total?: number;
  list?: object[];
}

// state 接口
export interface StateType {
  roleObj: tableType;
}

// modal 接口
export interface ModelType {
  namespace: string;
  state: any;
  reducers: {
    updateState: Reducer<StateType>;
  };
  effects: {
    getData: Effect;
  };
}

const initTableState = {
  pageSize: 10,
  pageIndex: 1,
  total: 0,
  list: [],
};

// modal 实现
// @ts-ignore
const Model: ModelType = {
  namespace: 'editorModel',
  state: {
    ...beJson(),
    testObj: null,
  },

  reducers: {
    updateState(state, { res }) {
      // 更新state
      return {
        ...state,
        ...res,
      };
    },
  },

  effects: {
    // 修改 state
    *updData({ payload }, { call, put, select }) {
      yield put({ type: 'updateState', res: payload });
    },

    // 获取
    *getData({ payload, mTitle, url }, { call, put, select }) {
      const { data } = yield call(services.postAction, url, payload);
      if (data) {
        yield put({ type: 'updateState', res: { [mTitle]: data } });
      }
      return data;
    },
  },
};

export default Model;
